package com.atguigu.gulimall.product.controller;

import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.exception.UserExistException;
import com.atguigu.gulimall.product.vo.UserRegistVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
@Slf4j
public class LoginController {


    @Autowired
    StringRedisTemplate redisTemplate;
    @GetMapping("/sms/sendcode")
    @ResponseBody
    public R sendCode(@RequestParam("phone") String phone){
        String redisCode = redisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX+":"+phone);
        if(!StringUtils.isEmpty(redisCode)){
            long curTime = System.currentTimeMillis();
            long l = Long.parseLong(redisCode.split("_")[1]);
            if(curTime - l < 60000){
                return R.error(BizCodeEnum.SMS_CODE_EXCEPTION.getCode(),BizCodeEnum.SMS_CODE_EXCEPTION.getMsg());
            }
        }
        String code = UUID.randomUUID().toString().substring(0, 5)+"_"+System.currentTimeMillis();
        redisTemplate.opsForValue().set(AuthServerConstant.SMS_CODE_CACHE_PREFIX+":"+phone,code,5, TimeUnit.MINUTES);
        //调用发短信接口
        log.info("验证码已发送、、");
        return R.ok();
    }

    @PostMapping("/regist")
    public void regist(@Valid  UserRegistVo vo , BindingResult br, Model model){
        //br就是校验发现的异常
        if(br.hasErrors()){
            Map<String,String> map =new HashMap<>();
            List<FieldError> fieldErrors = br.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                map.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
            model.addAttribute("errors",map);
        }
    }
    public void checkUserUnique(String User) throws UserExistException {
        //从数据库中查 二者比较 如果相等
        throw  new UserExistException();
    }
    public  void passwordBcrypt(String password){
        BCryptPasswordEncoder bp =new BCryptPasswordEncoder();
        //加密
        String encode = bp.encode(password);
        //然后存入数据库
        //用户登录时 输入密码 调用
        boolean boo = bp.matches("明文密码", encode);
        //如果返回true  说明密码添的是对的
    }
}
